﻿/// <reference path="jquery-1.4.2.min-vsdoc.js"/>
/// <reference path="../config/sw-config-default.js"/>
/// <reference path="sw-prototype.js"/>
/// <reference path="sw.js"/>

ictrcore = function () {
    /// <summary>Smart UI 控件核心支持库</summary>
}

///#region 公共对外方法
function linktoPage(link) {
    link = $(link);
    gotoPage(link.attr('ui'), link.attr('params'), link.attr('target'));
}
function gotoPage(ui, params, target) {
    if (ui) {
        if (target === '_blank')
            $sw.OpenPage(ui, params);
        else {
            if (iconfig.system.isloadUI)
                $sw.UI(ui);
            else
                $sw.GotoPage(ui, params);
        }
    }
}
function getAllPathUI() {
    var modal = $sw.globalParams.currentUI.split('_');
    for (var i = 1; modal[i]; i++) {
        modal[i] = modal[i - 1] + '_' + modal[i];
    }
    modal.shift();
    return modal;
}
///#endregion

///#region ajax处理
ictrcore.ajax = function (ajax) {
    /// <summary>执行ajax操作</summary>
    /// <param name="ajax" type="Json">ajax设置,包含：{url，data，fnsucess，fnerror}四个对象</param>
    /// <param name="selector" type="JQDomorDomStr">选择器对象</param>
    $sw.DO(ajax.url, ajax.data, ajax.fnsucess, ajax.fnerror);
}
ictrcore.DO = function (url, data, fnsucess, fnerror) {
    /// <summary>执行ajax操作</summary>
    /// <param name="url" type="String">请求的地址，此处为Function Code</param>    
    /// <param name="data" type="StringorJson">Post Data json字串或是Json对象</param>
    /// <param name="fnsucess" type="function">请求成功后的操作</param>
    /// <param name="fnerror" type="function">请求失败后的操作</param>
    $sw.DO(url, data, fnsucess, fnerror);
}
ictrcore.GetDatas = function (url, data, fnsucess, fnerror) {
    /// <summary>执行ajax操作</summary>
    /// <param name="url" type="String">请求的地址，此处为Function Code</param>    
    /// <param name="data" type="StringorJson">Post Data json字串或是Json对象</param>
    /// <param name="fnsucess" type="function">请求成功后的操作</param>
    /// <param name="fnerror" type="function">请求失败后的操作</param>
    $sw.DO(url, data, fnsucess, fnerror);
}
ictrcore.setAjax = function (ajax, oldajax) {
    /// <summary>设置选择控件弹出层</summary>
    /// <param name="ctr" type="JQDom">选择控件</param>
    /// <param name="selector" type="JQDomorDomStr">选择器对象</param>
    /// <param name="css" type="Json">样式对象</param>
    if (!oldajax)
        return ajax;
    if (ajax) {
        if (ajax.url)
            oldajax.url = ajax.url;
        if (hasValue(ajax.data))
            oldajax.data = ajax.data;
        if (ajax.fnsucess)
            oldajax.fnsucess = ajax.fnsucess;
        if (ajax.fnerror)
            oldajax.fnerror = ajax.fnerror;
    }
}
ictrcore.setPsaAjax = function (fncode, postdata, fnsucess, fnerror) {
    /// <summary>将psa的ajax设置转换为Jquery的ajax设置</summary>
    /// <param name="fncode" type="String">Function Code</param>    
    /// <param name="postdata" type="StringorJson">Post Data json字串或是Json对象</param>
    /// <param name="fnsucess" type="function">请求成功后的操作</param>
    /// <param name="fnerror" type="function">请求失败后的操作</param>
    /// <returns type="Json" />
    return { url: fncode, data: postdata, fnsucess: fnsucess, fnerror: fnerror, type: 'post' };
}
ictrcore.loadPage = function (uicode, container) {
    /// <summary>loadPage</summary>
    /// <param name="url" type="String">地址</param>
    /// <param name="container" type="JQDomorDomStr">填充的容器控件</param>
    $sw.LoadUI(uicode, container);
}
///#endregion

///#region 控件设置
ictrcore.upselector = function (ctr, selector, css, isclose, modalid) {
    /// <summary>设置选择控件弹出层</summary>
    /// <param name="ctr" type="JQDom">控件</param>
    /// <param name="selector" type="JQDomorDomStr">选择器对象</param>
    /// <param name="css" type="Json">样式对象</param>
    /// <param name="isclose" type="Bool">是否设置关闭</param>
    /// <param name="modalid" type="String">modal层id，设置时会根据modal的位置定位</param>
    /// <returns type="JQDom" />
    selector = $(selector);
    if (!selector.hasClass('upselector'))
        selector.addClass('upselector');

    var position = ctr.getPosition();
    if (modalid) {//设置modal层的位置
        var modalposition = ctr.parents('#' + modalid).position();
        if (modalposition) {
            position.top += modalposition.top;
            position.left += modalposition.left;
        }
        if (!selector.hasClass(iconfig.controls.cssonlyctr))
            selector.addClass(iconfig.controls.cssonlyctr)
    }
    selector.css({ "z-index": css.zindex, 'left': css.left + position.left, 'top': css.top + position.top + ctr.outerHeight()
    });
    if (!hasValue(isclose) || isclose)
        selector.mouseleave(function () { $(this).slideUp('normal'); });
    return selector;
}
ictrcore.upTip = function (ctr, tip, css, isclose) {
    /// <summary>设置选择控件弹出层</summary>
    /// <param name="ctr" type="JQDom">控件</param>
    /// <param name="tip" type="JQDom">选择器对象</param>
    /// <param name="css" type="Json">样式对象</param>
    /// <returns type="JQDom" />
    var position = ctr.getPosition(ctr);
    if (position.top == 0)
        position = ctr.parent().getPosition();
    tip.css({ "z-index": css.zindex, width: css.width, height: css.height
        , left: css.left + position.left + ctr.outerWidth(), top: css.top + position.top
        , opacity: css.opacity
    });
    if (!hasValue(isclose) || isclose)
        tip.click(function () { $(this).hide(); });
    return tip;
}
ictrcore.gotoNode = function (node, continer, isx, isy) {
    var nodePosition = node.position(), parentPostion = continer.offset(),
        top = (!hasValue(isy) || isy) ? nodePosition.top - continer.height() / 2 - parentPostion.top + continer.scrollTop() : null,
        left = (!hasValue(isx) || isx) ? nodePosition.left - continer.width() / 4 - parentPostion.left + continer.scrollLeft() : null;

    continer.animate({ scrollTop: top, scrollLeft: left }, 500);
}
ictrcore.setCenter = function (ctr) {
    /// <summary>设置选择弹出层页面居中效果</summary>
    /// <param name="ctr" type="JQDom">控件</param>
    var win = $(window);
    var width = parseInt(ctr.css('width'));
    var height = parseInt(ctr.css('height'));
    if (isNaN(width))
        width = ctr.outerWidth();
    if (isNaN(height))
        height = ctr.outerHeight();
    var scroll = $.getWinScroll();
    ctr.css({ left: (win.width() - width) / 2 + scroll.left, top: (win.height() - height) / 2 + scroll.top });
}
ictrcore.setShow = function (ctr) {
    /// <summary>设置选择弹出层显示样式</summary>
    /// <param name="ctr" type="JQDom">控件</param>
    var win = $(window);
    var width = parseInt(ctr.css('width'));
    var height = parseInt(ctr.css('height'));
    if (isNaN(width))
        width = ctr.outerWidth();
    if (isNaN(height))
        height = ctr.outerHeight();
    var scroll = $.getWinScroll();
    ctr.css({ left: win.width() - width + scroll.left - 10, top: win.height() + scroll.top - height  - 5 });
}

///#endregion

///#region Jquery Common Extend
$.extend({
    getWinScroll: function () {
        if ($.browser.msie || $.browser.mozilla)
            return { left: document.documentElement.scrollLeft, top: document.documentElement.scrollTop };
        else {
            var win = $(window);
            return { left: win.scrollLeft(), top: win.scrollTop() };
        }
    },
    getEventPosition: function (e) {
        var event = window.event || e;
        return position = { x: event.x || event.clientX, y: event.y || event.clientY };
    }
}); 

jQuery.fn.extend({
    getPosition: function () {
        var position = this.position();
//        if ($.browser.msie) {
//            var scroll = $.getWinScroll();
//            return { left: position.left + scroll.left, top: position.top + scroll.top };
//        }
        return position;
    },
    getCss: function (type) {
        var size = parseFloat(this.css(type));
        return isNaN(size) ? 0 : size;
    }
}); 
///#endregion

///#region loading
ictrcore.loading = function (panel) {
    ///<summary>页面Loading</summary>
    /// <param name="panel" type="JQSelector">需要loading的区域</param>
    if (!panel)
        panel = document.body;
    panel = $(panel);
    var position = panel.position(panel);
    var n = parseInt(Math.random() * 7);
    n = n == 1 ? '' : 'l' + n;
    panel.append($(document.createElement('div')).addClass('loading ' + n).css({ width: panel.width(), height: panel.height(),
        top: position.top, left: position.left
    }));
}
ictrcore.loadingClose = function (panel) {
    ///<summary>remove页面Loading</summary>
    /// <param name="panel" type="JQSelector">需要loading的区域</param>
    if (!panel)
        panel = document.body;
    $(panel).find('.loading').remove();
}
///#endregion

///#region modal
ictrcore.modal = function (type) {
    ///<summary>页面modal</summary>
    var doc = $(document);
    var fixwidth = 0, fixheight = 0;
    if ($.browser.msie && $.browser.version == 8)
        fixwidth = fixheight = -4;

    $(document.createElement('div')).attr('id', 'mdl_' + type).addClass('modal').css({ width: doc.width() + fixwidth, height: doc.height() + fixheight }).appendTo(document.body);
}
ictrcore.modalClose = function (type) {
    ///<summary>remove页面modal</summary>
    if(type)
        $('#mdl_' + type).remove();
    else
        $('.modal').remove();
    $('.' + iconfig.controls.cssonlyctr + ':visible').hide(); //处理其他非子类的唯一控件
}
///#endregion

///#region templatelist
ictrcore.teml = function (container, template, jsndatas) {
    /// <summary>根据模板数据生成列表Repeat</summary>    
    /// <param name="container" type="StringorJQSelect">容器的选择器的字串</param>
    /// <param name="template" type="StringorJQIDSelect">模板字串(如：<li lv="${Key}">${Value}</li>)或者是包括模板的对象选择器的ID,如:"#id"</param>
    /// <param name="jsndatas" type="Json">Json格式数据集合</param>
    return $.tmpl(template, jsndatas).appendTo(container);
}

ictrcore.temlbyAjax = function (container, template, url, data, fnsucess, fnerror) {
    /// <summary>根据模板数据生成列表Repeat</summary>    
    /// <param name="container" type="JQuerySelect">容器的选择器的字串</param>
    /// <param name="template" type="DomStrorJQueryIDSelect">模板字串(如：<li lv="${Key}">${Value}</li>)或者是包括模板的对象选择器的ID,如:"#id"</param>
    /// <param name="url" type="String">Function Code</param>    
    /// <param name="data" type="StringorJson">Post Data json字串或是Json对象</param>
    /// <param name="fnsucess" type="Function">成功后执行的操作,success(data,container),data：json数据、container容器控件</param>
    /// <param name="fnerror" type="function">请求失败后的操作</param>
    ictrcore.GetDatas(url, data,
    function (datas) {
        if (datas && datas.length) {
            var list = ictrcore.teml(container, template, datas);
            if (fnsucess)
                fnsucess(datas, list);
        }
    }, fnerror);
}

///#endregion
///#region button
ictrcore.button = function (btnid, name, text, type, props) {
    /// <summary>生产button</summary>
    /// <param name="btnid" type="String">button的id(不带前缀)</param>
    /// <param name="text" type="String">button的显示文本</param>
    /// <param name="type" type="String">button的类型;空为默认button，input:input button;img:img button；link：link button</param>
    /// <param name="name" type="String">公共button的定义类型名称</param>
    return $(ictrcore.button.getHtml(btnid, name, text, type, props));
}
ictrcore.button.getHtml = function (btnid, name, text, type, props) {
    /// <summary>生产button</summary>
    /// <param name="btnid" type="String">button的id(不带前缀)</param>
    /// <param name="text" type="String">button的显示文本</param>
    /// <param name="type" type="String">button的类型;空为默认button，input:input button;img:img button；link：link button</param>
    /// <param name="name" type="String">公共button的定义类型名称</param>
    var btn;
    if (!text && name)
        text = getJsonValue($iconst.controls.button, name);
    if (type) {
        switch (type) {
            case 'input':
                btn = iconfig.button.inputbtn.format(btnid, text, props);
                break;
            case 'img':
                btn = iconfig.button.imgbtn.format(btnid, text, props);
                break;
            case 'link':
                btn = iconfig.button.linkbtn.format(btnid, text, props);
                break;
            default:
                btn = iconfig.button.def.format(btnid, text);
                break;
        }
    }
    else
        btn = iconfig.button.def.format(btnid, text);
    return btn;
}
///#endregion


